package com.automation.ljf.repository.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.automation.ljf.repository.TestCaseRepository;
import com.automation.ljf.repository.impl.mapper.CaseMapper;
import com.automation.ljf.repository.impl.mapper.CaseReportMapper;
import com.automation.ljf.repository.impl.mapper.StepMapper;
import com.automation.ljf.repository.impl.mapper.StepReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.automation.ljf.converter.CaseConvert;
import com.automation.ljf.converter.CaseReportConvert;
import com.automation.ljf.converter.CaseStepConvert;
import com.automation.ljf.converter.CaseStepReportConvert;
import com.automation.ljf.domain.dataobject.CaseDO;
import com.automation.ljf.domain.dataobject.CaseReportDO;
import com.automation.ljf.domain.dataobject.CaseStepDO;
import com.automation.ljf.domain.dataobject.CaseStepReportDO;
import com.automation.ljf.domain.po.CasePO;
import com.automation.ljf.domain.po.CaseReportPO;
import com.automation.ljf.domain.po.CaseStepPO;
import com.automation.ljf.domain.po.CaseStepReportPO;
import com.automation.ljf.domain.vo.CaseStepReportVO;
import com.automation.ljf.domain.vo.CaseVO;
import com.automation.ljf.exception.BizException;
import com.automation.ljf.util.ResultMessage;

/**
 * 数据仓库实现类
 *
 * @author lijiefeng
 * @since 2021/8/25
 */
@Repository
public class TestCaseRepositoryImpl implements TestCaseRepository {

    @Autowired
    private StepMapper dbCaseStepProxy;

    @Autowired
    private CaseMapper dbCaseProxy;

    @Autowired
    private CaseReportMapper dbCaseReportProxy;

    @Autowired
    private StepReportMapper dbCaseStepReportProxy;

    @Autowired
    private CaseStepConvert caseStepConvert;

    @Autowired
    private CaseStepReportConvert caseStepReportConvert;

    @Autowired
    private CaseConvert caseConvert;

    @Autowired
    private CaseReportConvert caseReportConvert;


    @Override
    public ResultMessage selectCaseStepByCId(String caseId) {
        if (caseId == null){
            throw new BizException(400, "caseId不能为空");
        }
        //根据用例id查询用例步骤PO
        List<CaseStepPO> caseStepPOS = dbCaseStepProxy.findCaseStepByCId(caseId, null);
        PageInfo<CaseStepPO> pageInfo = new PageInfo<>(caseStepPOS);

        //将用例步骤PO转换为DO返回
        List<CaseStepDO> caseStepDOS = caseStepPOS.parallelStream().map(caseStepConvert::caseStepPOToDO).collect(Collectors.toList());
        return ResultMessage.ok().addData("caseStepDOS", caseStepDOS).addData("total", pageInfo.getTotal());
    }

    @Override
    public Integer insertCaseStep(CaseStepDO caseStepDO) {
        CaseStepPO caseStepPO = caseStepConvert.caseStepDOToPO(caseStepDO);
        CaseStepPO caseStep = dbCaseStepProxy.findCaseStepByCIdAndStepId(caseStepPO.getCaseId(), caseStepPO.getStepId());
        if (caseStep == null) {
            //数据库不存在该步骤序号，则插入到数据库
            return dbCaseStepProxy.insertCaseStep(caseStepPO);
        }
        return 0;
    }

    @Override
    public Integer selectStepIsInputByCaseId(String caseId) {
        List<CaseStepPO> caseStepPOS = dbCaseStepProxy.findCaseStepByCId(caseId, true);
        //dbCaseStepProxy.deleteStepByCaseId(caseId, null);
        return caseStepPOS.size();
    }

    @Override
    public Integer deleteStepByCaseId(String caseId, List<CaseStepDO> caseStepDOS) {
        List<CaseStepPO> caseStepPOS = caseStepDOS.parallelStream()
                .map(caseStepConvert::caseStepDOToPO).collect(Collectors.toList());
        return dbCaseStepProxy.deleteStepByCaseId(caseId, caseStepPOS);
    }

    @Override
    public CaseVO selectCaseByCaseId(String caseId) {
        CasePO casePO = dbCaseProxy.selectCaseByCaseId(caseId);
        //将测试用例PO转换为VO
        return caseConvert.casePOToVO(casePO);
    }

    @Override
    public Integer insertCase(CaseDO caseDO) {
        CasePO casePO = caseConvert.caseDOToPO(caseDO);
        return dbCaseProxy.insertCase(casePO);
    }

    @Override
    public ResultMessage selectCaseByPage(String admUserId, Integer page, Integer limit) {
        //进行物理分页
        PageHelper.startPage(page, limit);
        List<CasePO> casePOS;
        if (admUserId != null){
            casePOS = dbCaseProxy.selectCaseByPage(admUserId);
        }else {
            casePOS = new ArrayList<>();
        }
        PageInfo<CasePO> pageInfo = new PageInfo<>(casePOS);
        List<CaseVO> caseVOS = casePOS.stream().map(caseConvert::casePOToVO).collect(Collectors.toList());
        return caseVOS.size() > 0 ?
                ResultMessage.ok("查询成功")
                        .addData("caseVOS", caseVOS)
                        .addData("total", pageInfo.getTotal()) : ResultMessage.error("没有数据").addData("total", pageInfo.getTotal());
    }

    @Override
    public Integer updateCaseByCaseId(CaseDO caseDO) {
        CasePO casePO = caseConvert.caseDOToPOUpdate(caseDO);
        return dbCaseProxy.updateCaseByCaseId(casePO);
    }

    @Override
    public Integer deleteCaseByCaseId(List<CaseDO> caseDOS) {
        List<CasePO> casePOS = caseDOS.parallelStream().map(caseConvert::caseDOToPOUpdate).collect(Collectors.toList());
        int res = dbCaseProxy.deleteCaseByCaseId(casePOS);
        //同时把该用例的步骤进行删除
        dbCaseStepProxy.deleteStepByCaseId(casePOS.get(0).getCaseId(), null);
        return res;
    }

    @Override
    public Integer insertCaseReport(CaseReportDO caseReportDO, String reportId) {
        CaseReportPO caseReportPO = caseReportConvert.caseReportDOToPO(caseReportDO, reportId);
        return dbCaseReportProxy.insertCaseReport(caseReportPO);
    }

    @Override
    public ResultMessage selectCaseReportByUId(String admUserId) {
        List<CaseReportPO> caseReportPOS = dbCaseReportProxy.selectCaseReportByUId(admUserId);
        PageInfo<CaseReportPO> pageInfo = new PageInfo<>(caseReportPOS);
        List<CaseReportDO> caseReportDOS = caseReportPOS.stream().map(caseReportConvert::caseReportPOToDO)
                .sorted(Comparator.comparing(CaseReportDO::getCreateTime))
                .collect(Collectors.toList());
        return ResultMessage.ok("查询成功").addData("caseReportDOS", caseReportDOS).addData("total", pageInfo.getTotal());
    }

    @Override
    public Integer insertCaseStepReport(List<CaseStepReportDO> caseStepReportDOS, String reportId) {
        //CaseStepReport实体DO转换为PO
        List<CaseStepReportPO> caseStepReportPOS = caseStepReportDOS.stream()
                .map(caseStepReportDO -> {
                    return caseStepReportConvert.caseStepReportDOToPO(caseStepReportDO, reportId);
                })
                .sorted(Comparator.comparing(CaseStepReportPO::getStepId))
                .collect(Collectors.toList());
        return dbCaseStepReportProxy.insertCaseStepReport(caseStepReportPOS);
    }

    @Override
    public ResultMessage selectStepReportByCaseReportId(String reportId) {
        List<CaseStepReportPO> caseStepReportPOS = dbCaseStepReportProxy.selectStepReportByCaseReportId(reportId);
        PageInfo<CaseStepReportPO> pageInfo = new PageInfo<>(caseStepReportPOS);
        List<CaseStepReportVO> caseStepReportVOS = caseStepReportPOS.stream()
                .map(caseStepReportConvert::caseStepReportPOToVO)
                .sorted(Comparator.comparing(CaseStepReportVO::getStepId))
                .collect(Collectors.toList());
        return caseStepReportVOS.size() > 0 ?
                ResultMessage.ok("查询成功").addData("caseStepReportVOS", caseStepReportVOS)
                        .addData("total", pageInfo.getTotal()) : ResultMessage.error("没有数据").addData("total", pageInfo.getTotal());
    }


}
